﻿using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System;

namespace OrderService
{
    public static class ConsulHelper
    {
        /// <summary>
        /// 服务注册到consul
        /// </summary>
        /// <param name="app"></param>
        /// <param name="lifetime"></param>
        public static IApplicationBuilder RegisterConsul(this IApplicationBuilder app, IConfiguration configuration, IHostApplicationLifetime lifetime)
        {
            var consulClient = new ConsulClient(c =>
            {
                //consul地址
                c.Address = new Uri(configuration["ConsulSetting:ConsulAddress"]);
            });

            var registration = new AgentServiceRegistration();

            registration.ID = Guid.NewGuid().ToString();//服务实例唯一标识
            registration.Name = configuration["ConsulSetting:ServiceName"];//服务名
            registration.Address = configuration["ConsulSetting:ServiceIP"]; //服务IP
            registration.Port = int.Parse(configuration["ConsulSetting:ServicePort"]);//服务端口 因为要运行多个实例，端口不能在appsettings.json里配置，在docker容器运行时传入
            registration.Check = new AgentServiceCheck();

            registration.Check.DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5);//服务启动多久后注册
            registration.Check.Interval = TimeSpan.FromSeconds(10);//健康检查时间间隔
            registration.Check.HTTP = $"http://{configuration["ConsulSetting:ServiceIP"]}:{configuration["ConsulSetting:ServicePort"]}{configuration["ConsulSetting:ServiceHealthCheck"]}";//健康检查地址
            registration.Check.Timeout = TimeSpan.FromSeconds(5);//超时时间
           


            //服务注册
            consulClient.Agent.ServiceRegister(registration).Wait();

            //应用程序终止时，取消注册
            lifetime.ApplicationStopping.Register(() =>
            {
                consulClient.Agent.ServiceDeregister(registration.ID).Wait();
            });

            return app;
        }
    }
}
